package com.mobile.collect.activitys;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.mobile.collect.R;
import com.mobile.collect.dialogs.SubmitTipDialog;
import com.mobile.collect.present.SplashPresent;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NLog;
import cn.droidlover.xdroidmvp.kit.NToast;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;


public class SplashActivity extends XActivity<SplashPresent>{
    @Override
    public SplashPresent newP() {
        return new SplashPresent();
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public void getIntentData() {
    }

    @Override
    public void initView() {
        super.initView();
    }



    @Override
    public void initData(Bundle savedInstanceState) {
        getP().parserTypeInfo();
        checkPermission();
        SharedPref.getInstance(this).putBoolean("quanxian",false);
    }

    private void startTimer() {
        Observable observable = null;
        observable = Observable.timer(1000, TimeUnit.MILLISECONDS);
        observable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                if (isOutTime()) {
                    showUpdateVerson();
                }else{
                    goToMainActivity();
                }

            }
        });
    }

    private void showUpdateVerson() {
        SubmitTipDialog tipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://d.zqapps.com/jian2dang");
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void cancelClick() {

            }
        });
        tipDialog.display("提示", "版本已过期，请下载新版本！", "取消", "下载");
    }


    private void checkPermission() {
        final String[] PHONE = new String[]{Permission.READ_EXTERNAL_STORAGE,Permission.CAMERA,
                Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_PHONE_STATE};
        boolean hasPermission = AndPermission.hasPermissions(this, PHONE);
        NLog.e("SplashActivity", "checkPermission hasPermission = " + hasPermission);
        if (hasPermission) {
            startTimer();
        } else {
            // 申请权限。
            AndPermission.with(this)
                    .runtime()
                    .permission(PHONE)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            startTimer();
                        }
                    }).onDenied(new Action<List<String>>() {
                @Override
                public void onAction(List<String> data) {
                    startTimer();
                }
            }).start();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }



    private void goToMainActivity() {
        //863258031845258 863258031845266
        //863258031844814 863258031844822
        //863258031848930 863258031848948
        //863258031761034 863258031761042
        String[] imeis = new String[]{
                "863258031845258",
                "863258031845266",
                "863258031844814",
                "863258031844822",
                "863258031848930",
                "863258031848948",
                "863258031761034",
                "863258031761042",
        };
        String imei = Kits.PhoneInfo.getIMEI(this);
        boolean allowed = true;
        for (String item : imeis) {
            if (TextUtils.equals(item,imei)) {
                allowed = true;
                break;
            }
        }
        if (allowed) {
            startActivity(new Intent(this, MainActivity.class));
        }else{
            NToast.shortToast("无对应设备号");
            finish();
        }
//        startActivity(new Intent(this, MainActivity.class));

//        startActivity(new Intent(this, SquareGLSurfaceViewActivity.class));
//        startActivity(new Intent(this, CameraActivity.class));
//        finish();



    }

}
